package com.keepsport.keepsportapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keepsport.keepsportapi.dao.CommunityCircleDao;
import com.keepsport.keepsportapi.dao.CommunityLogDao;
import com.keepsport.keepsportapi.dto.CommunityCircleAddDto;
import com.keepsport.keepsportapi.dto.CommunityCircleDto;
import com.keepsport.keepsportapi.entity.CommunityCircle;
import com.keepsport.keepsportapi.entity.CommunityLog;
import com.keepsport.keepsportapi.service.intf.CommunityCircleService;
import com.keepsport.keepsportapi.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/***
 *         O
 *      _ooOoo_
 *     o8888888o
 *     88" . "88
 *     (| -_- |)
 *      O\ = /O
 *  ____/`---'\____
 *  ...............
 *  佛祖保佑  永无BUG
 *   Data:2022/7/1
 *   Author:King
 *   Description:
 */
@Service
public class CommunityCircleServiceImpl implements CommunityCircleService {
    @Resource
    private CommunityCircleDao dao;
    @Resource
    private CommunityLogDao logDao;
    @Override
    public R save(CommunityCircleAddDto dto, int token) {
        if (dto!=null){
            CommunityCircle communityCircle = new CommunityCircle();
            BeanUtils.copyProperties(dto,communityCircle);
            communityCircle.setUid(token);
            communityCircle.setCtime(new Date());
            communityCircle.setLikenum(0);
            if (dao.insert(communityCircle)>0){
                return R.ok("添加成功！");
            }
        }
        return R.fail();
    }

    @Override
    public R query() {
        List<CommunityCircleDto> communityCircleDtos = dao.queryAll();
        return R.ok(communityCircleDtos);
    }

    @Override
    public R selectLimit(int page, int size) {
        return R.ok(dao.selectLimit(page, size));
    }

    @Transactional
    @Override
    public R updataLikeNum(int id,int token) {
        //获取朋友圈
        if (id>0){
            CommunityCircle communityCircle = dao.selectById(id);
            if (communityCircle!=null){
                //查询日志是否有这个信息
                QueryWrapper<CommunityLog> wrapper = new QueryWrapper<>();
                wrapper.eq("cid",id);
                wrapper.eq("uid",token);
                CommunityLog communityLog = logDao.selectOne(wrapper);
                //判断日志表
                if (communityLog==null){
                    //说明没有点过赞
                    if (logDao.insert(new CommunityLog(id,token))>0){
                        Integer likenum = communityCircle.getLikenum()+1;
                        communityCircle.setLikenum(likenum);
                        dao.updateById(communityCircle);
                        return R.ok("点赞成功！");
                    }
                }else {
                    if (logDao.delete(wrapper)>0){
                        Integer likenum = communityCircle.getLikenum()-1;
                        communityCircle.setLikenum(likenum);
                        dao.updateById(communityCircle);
                        return R.fail("取消点赞！");
                    }
                }
            }
            return R.fail("此消息不存在！！");
        }
        return R.fail("非法参数！");
    }


    @Override
    public R queryMine(int token) {
        QueryWrapper<CommunityCircle> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",token);
        wrapper.orderByDesc("ctime");
        List<CommunityCircle> circles = dao.selectList(wrapper);
        return R.ok(circles);
    }

    @Override
    public R queryOne(int token, int id) {
        if (id>0){
            CommunityCircle communityCircle = dao.selectById(id);
            if (communityCircle!=null){
                List<CommunityCircleDto> communityCircleDtos = dao.findOneById(id);
                return R.ok(communityCircleDtos);
            }else {
                return R.fail("没有此信息！");
            }
        }
        return R.fail("非法参数！");

    }
    //    @Transactional
//    @Override
//    public R updataLikeNum(int id,int token) {
//        //获取朋友圈
//        if (id>0){
//            CommunityCircle communityCircle = dao.selectById(id);
//            if (communityCircle!=null){
//                //查询日志是否有这个信息
//                QueryWrapper<CommunityLog> wrapper = new QueryWrapper<>();
//                wrapper.eq("cid",id);
//                wrapper.eq("uid",token);
//                CommunityLog communityLog = logDao.selectOne(wrapper);
//                //判断日志表
//                if (communityLog==null){
//                    //说明没有点过赞
//                    if (logDao.insert(new CommunityLog(id,token,1))>0){
//                        Integer likenum = communityCircle.getLikenum()+1;
//                        communityCircle.setLikenum(likenum);
//                        dao.updateById(communityCircle);
//                        return R.ok("点赞成功！");
//                    }
//                }else if (communityLog.getFlag()==0){
//                    communityLog.setFlag(1);
//                    logDao.updateById(communityLog);
//                    //对主表的修改
//                    Integer likenum = communityCircle.getLikenum()+1;
//                    communityCircle.setLikenum(likenum);
//                    dao.updateById(communityCircle);
//                    return R.ok("点赞成功！");
//                }
//                else {
//                    communityLog.setFlag(0);
//                    logDao.updateById(communityLog);
//                    Integer likenum = communityCircle.getLikenum()-1;
//                    communityCircle.setLikenum(likenum);
//                    dao.updateById(communityCircle);
//                    return R.fail("取消点赞！");
//                }
//            }
//        }
//        return R.fail("此消息不存在！！");
//    }

}
